using Microsoft.AspNetCore.Mvc;
using ZR.Model.FacManage.Dto;
using ZR.Model.FacManage;
using ZR.Service.FacManage.IFacManageService;
using ZR.Admin.WebApi.Filters;
using System.Threading.Tasks;

//创建时间：2025-03-29
namespace ZR.Admin.WebApi.Controllers.FacManage
{
    /// <summary>
    /// 工厂信息
    /// </summary>
    [Verify]
    [Route("FacManage/FacFactory")]
    public class FacFactoryController : BaseController
    {
        /// <summary>
        /// 工厂信息接口
        /// </summary>
        private readonly IFacFactoryService _FacFactoryService;

        public FacFactoryController(IFacFactoryService FacFactoryService)
        {
            _FacFactoryService = FacFactoryService;
        }

        /// <summary>
        /// 查询工厂信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("pageList")]
        [ActionPermissionFilter(Permission = "fac:factory:list")]
        public IActionResult PageFacFactory([FromQuery] FacFactoryQueryDto parm)
        {
            var response = _FacFactoryService.GetPageList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 查询工厂信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "fac:factory:list")]
        public IActionResult GetList([FromQuery] FacFactoryQueryDto parm)
        {
            var response = _FacFactoryService.GetList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 查询工厂信息详情
        /// </summary>
        /// <param name="FactoryId"></param>
        /// <returns></returns>
        [HttpGet("{FactoryId}")]
        [ActionPermissionFilter(Permission = "fac:factory:query")]
        public IActionResult GetFacFactory(string FactoryId)
        {
            var response = _FacFactoryService.GetInfo(FactoryId);

            var info = response.Adapt<FacFactoryDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加工厂信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "fac:factory:add")]
        [Log(Title = "工厂信息", BusinessType = BusinessType.INSERT)]
        public IActionResult AddFacFactory([FromBody] FacFactoryDto parm)
        {
            var modal = parm.Adapt<FacFactory>().ToCreate(HttpContext);
            var response = _FacFactoryService.AddFacFactory(modal);
            return SUCCESS(response);
        }

        /// <summary>
        /// 更新工厂信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "fac:factory:edit")]
        [Log(Title = "工厂信息", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateFacFactory([FromBody] FacFactoryDto parm)
        {
            var modal = parm.Adapt<FacFactory>().ToUpdate(HttpContext);
            var response = _FacFactoryService.UpdateFacFactory(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除工厂信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "fac:factory:delete")]
        [Log(Title = "工厂信息", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteFacFactory([FromBody] string[] ids)
        {
            var response = _FacFactoryService.DeleteFacFactory(ids);
            return ToResponse(response);
        }

        /// <summary>
        /// 导出工厂信息
        /// </summary>
        /// <returns></returns>
        [Log(Title = "工厂信息", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "fac:factory:export")]
        public IActionResult Export([FromQuery] FacFactoryQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _FacFactoryService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "工厂信息", "工厂信息");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}